package com.init.engineering.controller;

import com.init.engineering.pages.PageResult;
import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pages.Result;
import com.init.engineering.pojo.Project;
import com.init.engineering.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/project")
@Api(tags = "工程主体控制层接口")   //Api作用于类
public class  ProjoctController {
    @Autowired(required = false)
    private ProjectService projectService;

    /**
     * 分页查询所有工程
     * @param queryPageBean
     * @return
     */
    @PostMapping("/all")
    @ApiOperation(value = "分页条件查询所有的工程",notes = "查询接口",httpMethod = "post")
    public PageResult findProjectsByPages(@RequestBody QueryPageBean queryPageBean){
        PageResult projectsByPage = projectService.findProjectsByPage(queryPageBean);
        return projectsByPage;
    }

    /**
     * 新增工程
     * @RequestBody 来接收前端传递给后端的json字符串中的数据,将该json数据对象转为java对象
     * @param project
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增工程",notes = "添加接口",httpMethod = "post")
    public Result addProject(@RequestBody Project project){
        //获取系统更新时间
        project.setUpdatetime(new Date());
        int row = projectService.saveProject(project);
        Result result = new Result();
        if(row > 0){
            result.setMessage("工程添加成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("工程添加失败，请重试");
        }
        return result;
    }

    /**
     * 删除工程
     * @param pid
     * @return
     */
    @GetMapping("/delete/{pid}")
    @ApiOperation(value = "删除工程",notes = "删除接口",httpMethod = "get")
    public Result deleteProject(@PathVariable("pid") int pid){
        int row = projectService.deleteProject(pid);
        Result result = new Result();
        if(row > 0){
            result.setMessage("工程删除成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("工程删除失败，请重试");
        }
        return result;
    }

    /**
     * 通过工程pid查找唯一工程信息
     * @param pid
     * @return
     */
    @GetMapping("/findProjectByPid")
    public Result findProjectByPid(int pid){
        Project project = projectService.findProjectByPid(pid);
        Result result = new Result();
        if (project != null){
            result.setFlag(true);
            result.setMessage("回显成功");
            result.setData(project);
        }else{
            result.setFlag(false);
            result.setMessage("数据回显失败");
        }
        return result;
    }

    /**
     * 进行工程的更新
     * @param project
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新工程",notes = "更新接口",httpMethod = "post")
    public Result updateProject(@RequestBody Project project){
        project.setUpdatetime(new Date());
        int row = projectService.updateProject(project);
        Result result = new Result();
        if(row > 0){
            result.setMessage("工程更新成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("工程更新失败，请重试");
        }
        return result;
    }

    /**
     * 查询所有工程
     * @return
     */
    @RequestMapping("/findProjects")
    public Result findProjects(){
        List<Project> projects = projectService.findProjects();
        Result result = new Result();
        if(projects != null){
            result.setData(projects);
            result.setFlag(true);
        }else{
            result.setFlag(false);
        }
        return result;
    }
}
